cmake_minimum_required(VERSION 3.16)
project(timesync CXX)

# C++ 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 头文件目录
set(PROJECT_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include)

# nlohmann/json 的根目录（注意用正斜杠）
if (WIN32)
    # Windows 下的本地路径
    set(JSON_INCLUDE_DIR "D:/timesync/include/json/include")
else()
    # Linux 下从以下路径中查找 nlohmann/json.hpp
    # 先查系统包路径，再查项目路径
    find_path(JSON_INCLUDE_DIR nlohmann/json.hpp
            PATHS
            /usr/include
            /usr/local/include
            ${CMAKE_SOURCE_DIR}/include/json/include
            PATH_SUFFIXES nlohmann
    )
endif()

# 如果没找到，给出清晰报错
if (NOT JSON_INCLUDE_DIR)
    message(FATAL_ERROR
            "Could not find nlohmann/json.hpp.\n"
            "Please either:\n"
            "  1. sudo apt install nlohmann-json3-dev, or\n"
            "  2. make sure it exists under ${CMAKE_SOURCE_DIR}/include/json/include/nlohmann/json.hpp"
    )
endif()


# 线程库（Linux/Unix 需要，Windows 上无害）
find_package(Threads REQUIRED)

# ================= 公共库 =================
add_library(timesync_core
        src/controller.cpp
        src/synth.cpp
        src/time_source.cpp
)

target_include_directories(timesync_core
        PUBLIC
        ${PROJECT_INCLUDE_DIR}
        ${JSON_INCLUDE_DIR}
)

if (WIN32)
    target_compile_definitions(timesync_core PUBLIC WIN32_LEAN_AND_MEAN)
    # 关键：WinSock 对所有依赖 timesync_core 的 target 可见
    target_link_libraries(timesync_core PUBLIC ws2_32)
endif()

# ================= 仿真程序 =================
add_executable(timesync_sim
        src/sim.cpp
)

target_link_libraries(timesync_sim PRIVATE timesync_core Threads::Threads)
target_include_directories(timesync_sim PRIVATE
        ${PROJECT_INCLUDE_DIR}
        ${JSON_INCLUDE_DIR}
)
if (WIN32)
    # 额外再私有链接一次，防止直接在该目标中包含 socket 调用
    target_link_libraries(timesync_sim PRIVATE ws2_32)
endif()

# ================= 合成工具 =================
add_executable(synth_main
        src/synth_main.cpp
)

target_link_libraries(synth_main PRIVATE timesync_core Threads::Threads)
target_include_directories(synth_main PRIVATE
        ${PROJECT_INCLUDE_DIR}
        ${JSON_INCLUDE_DIR}
)
if (WIN32)
    target_link_libraries(synth_main PRIVATE ws2_32)
endif()

# ================= 可选：单文件 main.cpp =================
add_executable(main_exec main.cpp
        include/sync_ntp_msg.h
        include/logical_clock.h
        include/synth_matlab_bridge.h
        include/ntp_logic_demo.h
        include/kz_io.h
        include/struct_resource.h
)
target_link_libraries(main_exec PRIVATE timesync_core Threads::Threads)
target_include_directories(main_exec PRIVATE
        ${PROJECT_SOURCE_DIR}/include
        ${JSON_INCLUDE_DIR}
)
if (WIN32)
    target_link_libraries(main_exec PRIVATE ws2_32)
endif()

# ============== 新增：controller_app（你的调试入口） ==============
# 若源文件不在 src/ 下，请把路径改成实际位置
add_executable(controller_app
        src/controller_app.cpp
        src/timestamp_bench.cpp
        include/lclk_dyn.h
        src/lclk_dyn.cpp
        include/shm_lc_map.h
        src/ShmLcReader.cpp
        src/sync_eval_segments.cpp
)
target_link_libraries(controller_app PRIVATE timesync_core Threads::Threads)
target_include_directories(controller_app PRIVATE
        ${PROJECT_INCLUDE_DIR}
        ${JSON_INCLUDE_DIR}
)
if (WIN32)
    target_link_libraries(controller_app PRIVATE ws2_32)
endif()

# ============== MinGW 下启用 big-obj（解决 .debug_frame 过大） ==============
if (MINGW)
    foreach(tgt timesync_core timesync_sim synth_main main_exec controller_app)
        target_compile_options(${tgt} PRIVATE -Wa,-mbig-obj)
    endforeach()
endif()

add_executable(tsbench
        src/timestamp_bench.cpp
)

target_include_directories(tsbench PRIVATE
        ${PROJECT_SOURCE_DIR}/include
        ${JSON_INCLUDE_DIR}
)

# 关键：链接已有的 timesync_core（它里面已经编了 src/time_source.cpp）
target_link_libraries(tsbench PRIVATE timesync_core Threads::Threads)

if (WIN32)
    target_link_libraries(tsbench PRIVATE ws2_32)
endif()

